package org.pjlib.common;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;


import org.pjlib.net.HttpUtils;
import org.pjlib.net.JsonCallBack;
import org.pjlib.net.RequestCall;

/**
 * Created by ws on 2019/04/17.
 */
public class BaseDialog extends Dialog implements JsonCallBack {
    public BaseDialog(Context context) {
        super(context);
    }

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }
    /**
     * 在设置setContentView后调用宽度充满
     * @param gravity
     * */
    protected void setMatch(int gravity){
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; //设置为屏幕宽度
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT; //设置为屏幕高度
        lp.dimAmount = 0.3f;//外围遮罩透明度0.0f-1.0f
        dialogWindow.setAttributes(lp);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0); //设置边距
        dialogWindow.setGravity(gravity);
    }
    @Override
    public void onBeforeRequest(RequestCall call) {

    }

    @Override
    public void onResponseError(Throwable t, RequestCall call) {

    }

    @Override
    public void onResponseSuccess(RequestCall call) {

    }

    @Override
    public void onFinishRequest(RequestCall call) {

    }

    @Override
    public void show() {
        if(getContext() instanceof Activity && ((Activity)getContext()).isDestroyed()){
            return;
        }
        try {
            super.show();
        }catch (Exception e){

        }
    }

    @Override
    public Object getNetTag() {
        return this;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpUtils.cancelTag(this);
    }
}
